001 /** 002 * Java Gui Builder - A library to build GUIs using an XML file. 003 * Copyright 2002, 2003 (C) François Beausoleil 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library; if not, write to the Free Software 017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018 */ 019 020 package jgb.handlers.swing; 021 022 import jgb.builder.TagHandler; 023 024 import java.awt.*; 025 import java.util.EmptyStackException; 026 027 /** 028 * @since 0.2.1a 029 * @author Francois Beausoleil, <a href="mailto:fbos@users.sourceforge.net">fbos@users.sourceforge.net</a> 030 */ 031 public abstract class AbstractControlTagHandler extends AbstractTagHandler { 032 /** 033 * The value of the type attribute which instantiates a radio menu item. 034 */ 035 public static final String TYPE_RADIO = "radio"; 036 037 /** 038 * The name of the type attribute. 039 */ 040 protected static final String ATTR_TYPE = "type"; 041 042 /** 043 * The name of the text attribute. 044 */ 045 protected static final String ATTR_TEXT = "text"; 046 047 /** 048 * The name of the mnemonic attribute. 049 */ 050 protected static final String ATTR_MNEMONIC = "mnemonic"; 051 052 /** 053 * The name of the accelerator attribute. 054 */ 055 protected static final String ATTR_ACCELERATOR = "accelerator"; 056 057 /** 058 * The name of the group attribute. 059 */ 060 protected static final String ATTR_GROUP = "group"; 061 062 /** 063 * The name of the reflabel attribute. 064 */ 065 protected static final String ATTR_REFLABEL = "reflabel"; 066 067 /** 068 * The name of the selected attribute. 069 */ 070 protected static final String ATTR_SELECTED = "selected"; 071 072 /** 073 * Convenience method that maps to 074 * {@link jgb.handlers.swing.AbstractTagHandler#pushCurrentObject(java.lang.String, java.lang.Object) pushCurrentObject(String, Object)}. 075 * This method acceps a {@link java.awt.Component Component} instead of 076 * a generic {@link java.lang.Object Object}. 077 */ 078 protected void pushCurrentControl(Component component) { 079 pushCurrentControl(component, null); 080 } 081 082 083 /** 084 * Convenience method that maps to 085 * {@link jgb.handlers.swing.AbstractTagHandler#pushCurrentObject(java.lang.String, java.lang.Object) pushCurrentObject(String, Object)}. 086 * This method acceps a {@link java.awt.Component Component} instead of 087 * a generic {@link java.lang.Object Object}. 088 */ 089 protected void pushCurrentControl(Component component, String id) { 090 if (!isCurrentControlValid()) { 091 throw new EmptyStackException(); 092 } 093 094 Container parent = (Container)getCurrentControl(); 095 Object constraints = getCurrentConstraints(); 096 if (parent.getLayout() instanceof GridBagLayout) { 097 GridBagLayout layout = (GridBagLayout)parent.getLayout(); 098 layout.setConstraints(component, (GridBagConstraints)constraints); 099 parent.add(component); 100 } else { 101 parent.add(component, constraints); 102 } 103 104 super.pushCurrentObject(id, component); 105 } 106 107 /** 108 * Convenience method that maps to 109 * {@link jgb.handlers.swing.AbstractTagHandler#popCurrentObject() popCurrentObject()}. 110 */ 111 protected void popCurrentControl() { 112 super.popCurrentObject(); 113 } 114 115 /** 116 * Convenience method that automatically casts the current object to a 117 * {@link java.awt.Component Component}. 118 */ 119 protected Component getCurrentControl() { 120 return (Component)super.getCurrentObject(); 121 } 122 123 /** 124 * Convenience method that maps directly to 125 * {@link jgb.handlers.swing.AbstractTagHandler#getCurrentObjectId() getCurrentObjectId()}. 126 */ 127 protected String getCurrentControlId() { 128 return super.getCurrentObjectId(); 129 } 130 131 /** 132 * Convenience method that maps directly to 133 * {@link jgb.handlers.swing.AbstractTagHandler#isCurrentObjectValid() isCurrentObjectValid()}. 134 */ 135 protected boolean isCurrentControlValid() { 136 return super.isCurrentObjectValid(); 137 } 138 139 /** 140 * Convenience method that returns the context's current value in the 141 * {@link jgb.builder.TagHandler#CURRENT_CONSTRAINTS_KEY TagHandler.CURRENT_CONSTRAINTS_KEY} 142 * key. 143 */ 144 protected Object getCurrentConstraints() { 145 return tagContext.get(TagHandler.CURRENT_CONSTRAINTS_KEY); 146 } 147 148 /** 149 * Convenience method that prefixes the passed base class name with the 150 * subclass' implementation of {@link #getDefaultPackagePrefix()}. 151 * This method will not prefix the base class name with anything if 152 * it contains a period ('.').<br /> 153 * For instance, if <var>baseClassName</var> equals "<code>java.awt.Button</code>", 154 * it will return "<code>java.awt.Button</code>".<br /> 155 * But, if <var>baseClassName</var> equals "<code>JButton</code>" and 156 * {@link #getDefaultPackagePrefix()} returns "<code>javax.swing.</code>", 157 * then this method will return "<code>javax.swing.JButton</code>".<br /> 158 * Please note that this method will not be able to return a class name 159 * from the default package, unless the specific subclass returns 160 * "" from {@link #getDefaultPackagePrefix()}. 161 * @param baseClassName A class name, with or without a package. 162 * @return A fully qualified class name, with the default package 163 * prefixed to the base class name if none was specified. 164 */ 165 protected String calcControlClassName(String baseClassName) { 166 String className = baseClassName; 167 if (-1 == className.indexOf('.')) { 168 String prefix = getDefaultPackagePrefix(); 169 if (prefix.endsWith(".") == false) { 170 prefix = prefix + "."; 171 } 172 173 className = prefix + className; 174 } 175 176 return className; 177 } 178 179 /** 180 * Returns the default package in which to search for class names. 181 * This method supplies the missing information in 182 * {@link #calcControlClassName(java.lang.String) calcControlClassName(String)}. 183 */ 184 protected abstract String getDefaultPackagePrefix(); 185 }